﻿using DevExpress.Xpf.Charts;
using System;
using System.Globalization;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;


namespace TeBigCommonUI.Converter
{
    public class BaseAxisRotateConverter
    {
        public static readonly int kLabelCount = 5;

        public static readonly double kLineHeight = 27;

        public string GetShortText(string txt)
        {
            return txt;
        }
    }

    public class AxisRotateFontConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is AxisLabelItem item)
            {
                return item.Label.FontSize * 0.9;
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class AxisRotateTextConverter : BaseAxisRotateConverter, IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is AxisLabelItem item)
            {
                var str = item.Content.ToString();

                if (double.TryParse(str, out double dval))
                {
                    return str;
                }

                str = GetShortText(str);

                StringBuilder sb = new StringBuilder();
                int i = 0;
                for (i = 0; i < str.Length; i++)
                {
                    if (i < (str.Length - 1))
                    {
                        sb.Append(str[i]);
                        sb.Append("\n");
                    }
                    else
                    {
                        sb.Append(str[i]);
                    }
                }

                return sb.ToString();
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class AxisRotateWidthConverter : BaseAxisRotateConverter, IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is AxisLabelItem item)
            {
                var str = GetShortText(item.Content.ToString());

                double width = 0;

                for (int i = 0; i < str.Length; i++)
                {
                    FormattedText ft = new FormattedText(str[i].ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
                         new Typeface(item.Label.FontFamily, item.Label.FontStyle, item.Label.FontWeight, item.Label.FontStretch),
                         item.Label.FontSize, item.Label.Foreground);

                    width += ft.Height;
                }

                return width;
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class AxisRotateLineHeightConverter : BaseAxisRotateConverter, IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is AxisLabelItem item)
            {
                var str = GetShortText(item.Content.ToString());

                if (!string.IsNullOrWhiteSpace(str))
                {
                    FormattedText ft = new FormattedText(str[str.Length - 1].ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
                        new Typeface(item.Label.FontFamily, item.Label.FontStyle, item.Label.FontWeight, item.Label.FontStretch),
                        item.Label.FontSize, item.Label.Foreground);

                    return (kLineHeight * kLabelCount - ft.Height) / (str.Length - 1);
                }

                return kLineHeight;
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}